/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-16
 * Time: 11:03
 */
public class MyQueue {
    static class ListNode{
        int val;
        ListNode prev; //前驱
        ListNode next; //后驱

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;//头节点
    private ListNode last;//尾节点
    private int size = 0;

    //入队列
    public void offer(int data) {
        ListNode cur = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = cur;
        }else {
            this.last.next = cur;
            cur.prev = this.last;
            this.last = cur;
        }
        this.size++;
    }

    //出队列
    public int poll() {
        if (isEmpty()) {
            throw new EmptyQueueException("队列为空！");
        }
        int val = this.head.val;
        this.head = this.head.next;
        if (this.head == null) {
            this.last = null;
        }else {
            this.head.prev = null;
        }
        this.size--;
        return val;
    }
    //检测队列是否为空
    public boolean isEmpty() {
        return this.head == null;
    }

    //获取队头元素
    public int peek() {
        if (isEmpty()) {
            throw new EmptyQueueException("队列为空！");
        }
        return this.head.val;
    }

    //获取队列中有效元素个数
    public int size() {
        return this.size;
    }
}
